/** @format */

// 菜单模型
// Author 区区电脑
// Email 1573580882@qq.com
// Date 2023-01-28
// Copyright © 2023 gys. All rights reserved.
import { arrayToTree } from "@/utils/utils";

export default class MenuModel {
	// id
	id = null;
	// 上一级 id
	pId = null;
	// 名称
	name = null;
	// 编号
	code = null;
	// 类型 0=目录 1=页面 2=按钮或接口
	type = null;
	// 打开类型 1=组件 2=内链 3=外链
	openType = null;
	// 路径
	path = null;
	// 内外链地址
	link = null;
	// 权限
	permission = null;
	// 是否可见
	isShow = null;
	// 图标
	icon = null;
	// 子菜单
	childrens = null;
	constructor(options) {
		this.id = options.id;
		this.pId = options.pid;
		this.name = options.name;
		this.code = options.code;
		this.type = options.type;
		this.openType = options.openType;
		this.path = options.path;
		this.link = options.link;
		this.permission = options.permission;
		this.isShow = options.visible;
		this.icon = options.icon;
	}
	// 数组映射
	static Mapping(arr) {
		const array = [];
		for (let i = 0; i < arr.length; i++) {
			const item = arr[i];
			array.push(new MenuModel(item));
		}
		const tree = arrayToTree(
			array.filter(item => item.type < 2),
			"id",
			"pId",
			"childrens"
		);
		return { array, tree };
	}
}
